package com.woniuxy.education.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.education.entity.Article;
import com.woniuxy.education.entity.PageResult;
import com.woniuxy.education.entity.SelectArticle;
import com.woniuxy.education.mapper.ArticleMapper;
import com.woniuxy.education.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author author
 * @create 2021/4/2
 */

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    ArticleMapper articleMapper;

    // 封装分页信息
    public <T> PageResult<T> getResult(int page, int size, Page<T> pageHelper, List<T> t) {
        // 获取分页信息
        PageResult<T> pageResult = new PageResult<>();
        // 当前页
        pageResult.setCurrentPage(page);
        // 每一页大小
        pageResult.setSize(size);
        // 总条数
        pageResult.setTotal(pageHelper.getTotal());
        // 总页数
        pageResult.setTotalPage(pageHelper.getPages());
        // 查询的数据
        pageResult.setDatas(t);

        return pageResult;
    }

    @Override
    public Article findById(int id) {

        return articleMapper.findById(id);
    }

    @Override
    //所有文章分页显示
    public PageResult<Article> findArticleByPage(SelectArticle selectArticle) {
        if(selectArticle.getState()==4){
            selectArticle.setState(null);
        }
        if(selectArticle.getType()==10){
            selectArticle.setType(null);
        }
        // 分页
        Page<Article> pageHelper = PageHelper.startPage(selectArticle.getPage(), selectArticle.getSize());
        List<Article> articles = articleMapper.findAllArticle(selectArticle);

        return getResult(selectArticle.getPage(), selectArticle.getSize(), pageHelper, articles);
    }

    //多条件查询
//    @Override
//    public PageResult<Article> findByIf(int page, int size, int state, int type, String article_name) {
//
//        // 分页
//        Page<Article> pageHelper = PageHelper.startPage(page, size);
//        List<Article> articles = articleMapper.findByIf(state, type, "%"+article_name+"%");
//
//        return getResult(page, size, pageHelper, articles);
//    }

    @Override
    public boolean addArticle(Article article) {
        return articleMapper.addArticle(article);
    }

    @Override
    public boolean updateOneState(int id) {
       return articleMapper.updateOneState(id);
    }

    @Override
    public boolean updateTwoState(int id) {
        return articleMapper.updateTwoState(id);
    }

    @Override
    public boolean updateThreeState(int id) {
        return articleMapper.updateThreeState(id);
    }
}
